package com.macllvm.whale.adapter;

import java.util.List;
import android.content.Context;
import android.database.DataSetObserver;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.BaseAdapter;
import android.widget.TextView;
import com.macllvm.whale.entity.*;
import com.macllvm.whale.R;

public class MessageAdapter extends  BaseAdapter {
    private List<Message> msgList;
    private LayoutInflater inflater;
    private String[] msgTip;
    static class MessageItemView {
        TextView dateTime;
        TextView content;
    }



    public MessageAdapter(Context ctx) {
        inflater = LayoutInflater.from(ctx);
        msgTip = new String[]{ctx.getString(R.string.msg_tip_file_recv), ctx.getString(R.string.msg_tip_file_send)};
    }

    public void setDataSource(List<Message> list) {
        msgList = list;
    }

    @Override
    public int getCount() {
        return msgList.size();
    }

    @Override
    public Object getItem(int position) {
        return msgList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Message item = msgList.get(position);

        LinearLayout layout = new LinearLayout(parent.getContext());
        int mgsType = item.getType();
        if(mgsType == Message.MESSAGE_IN || mgsType == Message.MESSAGE_TIP_IN ) {
            inflater.inflate(R.layout.message_in, layout, true);
        }else if(mgsType == Message.MESSAGE_OUT || mgsType == Message.MESSAGE_TIP_OUT){
            inflater.inflate(R.layout.message_out, layout, true);
        }

        TextView date = (TextView) layout.findViewById(R.id.msg_date);
        date.setText(item.getDate());

        TextView content = (TextView) layout.findViewById(R.id.msg_content);
        if(mgsType != Message.MESSAGE_TIP_OUT && mgsType != Message.MESSAGE_TIP_IN) {
            content.setText(item.getText());
        }else{
            if(mgsType == Message.MESSAGE_TIP_IN)
                content.setText(msgTip[0] + item.getText());
            else
                content.setText(msgTip[1] + item.getText());
        }

        return layout;
    }

}